<?php
/* 
 * Copyright (c) 2011 Antoine d'Otreppe de Bouvette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

class ReflectionDocumentedClass extends ReflectionClass {
    /**
     * @var DocComment
     */
    private $docComment;
    
    /**
     * @param mixed $argument 
     */
    public function __construct($argument) {
        parent::__construct($argument);
    }
    
    public function getDeclaredMethods($filter = null) {
        $result = array();
        
        foreach ($this->getMethods($filter) as $method) {
            if ($method->getDeclaringClass()->getName() === $this->getName()) {
                $result[] = $method;
            }
        }
        
        return $result;
    }
    
    /**
     * @param string $filter
     * @return ReflectionDocumentedMethod[]
     */
    public function getMethods($filter = null) {
        if ($filter === null) {
            $methods = parent::getMethods();
        }
        else {
            $methods = parent::getMethods($filter);
        }
        
        $result = array();
        
        foreach ($methods as $method) {
            // Is this the getter or setter for a property ?
            if (preg_match('/^(?:g|s)et([A-Z][\w\d]*)$/', $method->name, $match)){
                if ($this->hasProperty(lcfirst($match[1]))) {
                    // This is a property, skip
                    continue;
                }
            }
                
            $result[] = new ReflectionDocumentedMethod(
                    $method->class, $method->name);
        }
        
        return $result;
    }
    
    /**
     * @param int $filter 
     * @return ReflectionDocumentedProperty[]
     */
    public function getProperties($filter = null) {
        if ($filter === null) {
            $properties = parent::getProperties();
        }
        else {
            $properties = parent::getProperties($filter);
        }
        
        $result = array();
        
        foreach ($properties as $property) {
            // Is there any getter / setter for this property ?
            $name = $property->getName();
            $getter = 'get'.ucfirst($name);
            $setter = 'set'.ucfirst($name);
            
            $accessor = null;
            $mutator = null;
            
            if ($this->hasMethod($getter)) {
                $accessor = $this->getMethod($getter);
            }
            if ($this->hasMethod($setter)) {
                $mutator = $this->getMethod($setter);
            }
            
            $result[] = new ReflectionDocumentedProperty(
                    $property->class, $property->name,
                    $accessor, $mutator);
        }
        
        return $result;
    }
    
    /**
     * @param string $name
     * @return ReflectionDocumentedMethod 
     */
    public function getMethod($name) {
        return new ReflectionDocumentedMethod($this->getName(), $name);
    }
    
    /**
     * @return string
     */
    public function getSummary() {
        return $this->getDocComment()->getBrief();
    }
    
    /**
     *
     * @return string
     */
    public function getDescription() {
        return $this->getDocComment()->getDescription();
    }
    
    /**
     * @return DocComment
     */
    public function getDocComment() {
        if ($this->docComment === null) {
            $this->docComment = DocComment::parseDocComment(
                    parent::getDocComment());
        }
        
        return $this->docComment;
    }
    
    /**
     * Returns the source code of this file.
     * 
     * @return string 
     */
    public function getCode() {
        // Not the cleanest code we could write
        // But the files will never be huge...
        $start = $this->getStartLine() - 1;
        $length = $this->getEndLine() - $start;
        
        $lines = file($this->getFileName());
        $code = implode('', array_slice($lines, $start, $length));
        $docComment = parent::getDocComment();
        
        if ($docComment) {
            return parent::getDocComment() . PHP_EOL . $code;
                
        }
        else {
            return $code;
        }
    }
}
